﻿using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using MockSchoolManagement.Models;
using MockSchoolManagement.ViewModels;

namespace MockSchoolManagement.Controllers {
    [AllowAnonymous]
    public class AccountController : Controller {
        private UserManager<IdentityUser> _userManager;
        private SignInManager<IdentityUser> _signInManager;

        public AccountController (UserManager<IdentityUser> userManager,
            SignInManager<IdentityUser> signInManager) {
            this._userManager = userManager;
            this._signInManager = signInManager;
        }

        [HttpGet]
        public IActionResult Register () {
            return View ();
        }

        [HttpPost]
        public async Task<IActionResult> Register (RegisterViewModel model) {
            if (ModelState.IsValid) {
                //将数据从RegisterViewModel复制到IdentityUser
                var user = new IdentityUser {
                    UserName = model.Email,
                    Email = model.Email,

                };
                //将用户数据存储在AspNetUsers数据库表中
                var result = await _userManager.CreateAsync (user, model.Password);
                //如果成功创建用户，则使用登录服务登录用户信息
                //并重定向到home econtroller的索引操作
                if (result.Succeeded) {
                    await _signInManager.SignInAsync (user, isPersistent : false);
                    return RedirectToAction ("index", "home");
                }
                //如果有任何错误，将它们添加到ModelState对象中
                //将由验证摘要标记助手显示到视图中
                foreach (var error in result.Errors) {
                    ModelState.AddModelError (string.Empty, error.Description);
                }
            }

            return View (model);
        }

        [HttpPost]
        public async Task<IActionResult> Logout () {
            await _signInManager.SignOutAsync ();
            return RedirectToAction ("index", "home");
        }

        [HttpGet]
        [AllowAnonymous]
        public IActionResult Login () {

            return View ();
        }

        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> Login (LoginViewModel model, string returnUrl) {
            if (ModelState.IsValid) {
                var result = await _signInManager.PasswordSignInAsync (
                    model.Email, model.Password, model.RememberMe, false);

                if (result.Succeeded) {
                    if (!string.IsNullOrEmpty (returnUrl)) {
                        if (Url.IsLocalUrl (returnUrl)) {
                            return Redirect (returnUrl);
                        }
                    } else {
                        return RedirectToAction ("index", "home");
                    }
                }

                ModelState.AddModelError (string.Empty, "登录失败，请重试");
            }

            return View (model);
        }

        [AcceptVerbs ("Get", "Post")]
        [AllowAnonymous]
        public async Task<IActionResult> IsEmailInUse (string email) {
            var user = await _userManager.FindByEmailAsync (email);

            if (user == null) {
                return Json (true);
            } else {
                return Json ($"邮箱： {email} 已经被注册使用了。");
            }

        }
    }
}